package com.tjbaobao.videoplayol.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;

import com.tjbaobao.videoplayer.view.base.BaseProgressView;

/**
 * 自定义视频播放器 顶部 进度条
 * @author TJbaobao
 *
 */
public class LodingProgressView extends BaseProgressView {
	private Context context ;
	private float progress=0;
	private int backgroundColor= Color.parseColor("#eb3953") ;//默认背景颜色
	private int progressColor = Color.parseColor("#eb3953");//默认进度条颜色
	private int progressBGColor = Color.parseColor("#c9c9c9");//默认进度条背景颜色
	private int textColor = Color.parseColor("#ffffff");//默认进度条背景颜色
	private boolean bStart ;
	private String text = "";
	private int textSize = 20;
	private int viewWidth,viewHeight;
	private boolean bFinish = false;
	private float animPosition = 0 ;
	public LodingProgressView(Context context) {
		super(context);
	}
	public LodingProgressView(Context context, AttributeSet attrs)
	{
		super(context ,attrs);
		this.context = context ;
	}
	public LodingProgressView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context ,attrs,defStyle);
		this.context = context ;
	}
	@Override
	protected void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		paint.setAntiAlias(true);// 消除锯齿效果
		
		if(bStart)
		{
			//启动进度条
			canvas.drawColor(progressBGColor);
			paint.setColor(progressColor);
			paint.setStyle(Paint.Style.FILL);
			float width = progress*(float)viewWidth;
			canvas.drawRect(0, 0, width, viewHeight, paint);
	       if(animPosition-width/2>width)
	       {
	    	   animPosition = 0;
	       }
	       else
	       {
	    	   animPosition+=viewWidth/100;
	       }
	        LinearGradient lg= null ;
	        
	       
	        if(animPosition>width/2)
	        {
	        	 lg = new LinearGradient(animPosition-width/2,0,animPosition,0,progressColor, Color.WHITE, Shader.TileMode.MIRROR);
	        }
	        else
	        {
	        	 lg = new LinearGradient(0,0,animPosition,0,progressColor, Color.WHITE, Shader.TileMode.MIRROR);
	        }
	        paint.setShader(lg);
	        canvas.drawRect(animPosition-width/2, 0, animPosition, viewHeight, paint);
	        paint.setShader(null);
	        paint.setColor(progressBGColor);
	        canvas.drawRect(width, 0, viewWidth, viewHeight, paint);
	        invalidate();
	        
		}
		else
		{
			//未启动进度条
			canvas.drawColor(progressColor);
			paint.setColor(Color.WHITE);
			drawTextCenter(canvas,text);
		}
		
	}

	@Override
	public void setProgress(float progress) {
		if(!bStart)
		{
			bStart = true;
		}
		this.progress = progress ;
		postInvalidate();
	}
	public void setError(String text)
	{
		bStart = false;
		this.text = text;
		postInvalidate();
	}
	
	public void finish()
	{
		bFinish = true;
		postInvalidate();
	}
	private void drawTextCenter(Canvas canvas, String text)
	{
		Paint mPaint = new Paint();
		mPaint.setStrokeWidth(3);
		mPaint.setTextSize(textSize);
		mPaint.setColor(textColor);
		mPaint.setTextAlign(Align.LEFT);
		Rect bounds = new Rect();
		mPaint.getTextBounds(text, 0, text.length(), bounds);
		FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
		int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
		canvas.drawText(text, getMeasuredWidth() / 2 - bounds.width() / 2, baseline, mPaint);
	}
	private int getColorForRes(int resID)
	{
		return context.getResources().getColor(resID);
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		this.viewWidth = measureWidth(widthMeasureSpec);  
        this.viewHeight = measureHeight(heightMeasureSpec);  
        setMeasuredDimension(viewWidth, viewHeight);
	}
	/**
	 * 计算组件宽度
	 */
	private int measureWidth(int measureSpec) {
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		int result = 500;
		if (specMode == MeasureSpec.AT_MOST) {
			result = specSize;
		}
		else if (specMode == MeasureSpec.EXACTLY) {

			result = specSize;
		}
		return result;
	}

	/**
	 * 计算组件高度
	 */
	private int measureHeight(int measureSpec) {

		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		int result = 500;
		if (specMode == MeasureSpec.AT_MOST) {
			result = specSize;
		} else if (specMode == MeasureSpec.EXACTLY) {

			result = specSize;
		}
		return result;
	}
	public void setMBackgroundColor(int backgroundColor) {
		this.backgroundColor = backgroundColor;
		postInvalidateDelayed(1); 
	}
	public int getProgressColor() {
		return progressColor;
	}
	public void setProgressColor(int progressColor) {
		this.progressColor = progressColor;
	}
	public int getProgressBGColor() {
		return progressBGColor;
	}
	public void setProgressBGColor(int progressBGColor) {
		this.progressBGColor = progressBGColor;
	}
	public int getTextColor() {
		return textColor;
	}
	public void setTextColor(int textColor) {
		this.textColor = textColor;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
		postInvalidateDelayed(1); 
	}
	public int getTextSize() {
		return textSize;
	}
	public void setTextSize(int textSize) {
		this.textSize = textSize;
	}
	
}
